import os

def TxtAN2TxtU8(dir):
    '''
    这里设计了使用chardet,实际上也可以不考虑它,注释掉即可
    '''
    target = []
    target_dir = os.listdir(dir)
    for each in target_dir:
        if each.endswith(".txt") and not each.endswith("_utf8.txt"):
            target.append(each[:-4])#删去尾缀,后面增加新缀名
    print('what did we get: ',target)
    for each in target:
        with open(dir+'/'+each+'.txt', "r", encoding='ANSI') as f:  #打开文本
            data = f.read()   #读取文本
            with open(dir+'/'+each+'_utf8.txt', "w", encoding='utf-8') as nf:
                nf.write(data)
                print('saved',dir+'/'+each+'_utf8.txt')

TxtAN2TxtU8('./data')